package guanqu.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import guanqu.beans.IrrBDDeviceDetail;
import helpers.BeanHelper;
import manager.PersistentManager;

/**
 * 获取设备信息
 * Servlet implementation class GetDeviceBasicInfo
 */
//@WebServlet("/GetDeviceBasicInfo")
public class GetDeviceBasicInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public static int MAX = 1;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetDeviceBasicInfo() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		List<IrrBDDeviceDetail> irrBDDeviceDetails = new ArrayList<IrrBDDeviceDetail>();
		
		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data,Upload.class);
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		Session session = sf.getCurrentSession();
		
		Transaction tx = session.beginTransaction();
		try
		{
			Query q = null;
//			if (upload.getFlag() == 1) {
//				q = session.createQuery("from IrrBDDeviceDetail");
//			}else if (upload.getFlag() == 2) {
//				q = session.createQuery("from IrrBDDeviceDetail dd where dd.devType = ?");
//				q.setParameter(0, upload.getDevType());
//			}else if (upload.getFlag() == 3) {
//				q = session.createQuery("from IrrBDDeviceDetail dd where dd.devState = ?");
//				q.setParameter(0, upload.getDevState());
//			}
			
			if (upload.getDevState().equals("全部") && upload.getDevType().equals("000")) {
				q = session.createQuery("select new IrrBDDeviceDetail(devName,devType,manufacture,devState,locCode) "
						+ "from IrrBDDeviceDetail");
			}
			else if (upload.getDevState().equals("全部")) {
				q = session.createQuery("select new IrrBDDeviceDetail(devName,devType,manufacture,devState,locCode) from IrrBDDeviceDetail dd where dd.devType = ?");
				q.setParameter(0, upload.getDevType());
			}
			else if (upload.getDevType().equals("000")) {
				q = session.createQuery("select new IrrBDDeviceDetail(devName,devType,manufacture,devState,locCode) from IrrBDDeviceDetail dd where dd.devState = ?");
				q.setParameter(0, upload.getDevState());
			}else 
			{
				q = session.createQuery("select new IrrBDDeviceDetail(devName,devType,manufacture,devState,locCode) from IrrBDDeviceDetail dd where dd.devState = ? and dd.devType = ?");
				q.setParameter(0, upload.getDevState());
				q.setParameter(1, upload.getDevType());
			}
			
			
			// 设置500条限制
			q.setMaxResults(MAX);
			q.setFirstResult(0);
			q.setFirstResult(upload.getFrom());
			
			
			irrBDDeviceDetails = q.list();
			download.setDetails(irrBDDeviceDetails);
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		}catch(Exception e)
		{
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		}
		finally {
//			session.close();
		}
		
		for(IrrBDDeviceDetail detail:irrBDDeviceDetails)
		{
			try {
				BeanHelper.beanAttributeValueTrim(detail);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// 上行数据
	public static class Upload
	{
		// 1,全部。2，按照类型。3，按照状态
		int flag;
		String devType;
		String devState;
		
		int from;
		public int getFlag() {
			return flag;
		}
		public void setFlag(int flag) {
			this.flag = flag;
		}
		public String getDevType() {
			return devType;
		}
		public void setDevType(String devType) {
			this.devType = devType;
		}
		public String getDevState() {
			return devState;
		}
		public void setDevState(String devState) {
			this.devState = devState;
		}
		public int getFrom() {
			return from;
		}
		public void setFrom(int from) {
			this.from = from;
		}
		
		
	}
	
	
	// 下行数据
	public static class Download
	{
		int status;
		
		// 基本设备信息
		List<IrrBDDeviceDetail> details = new ArrayList<IrrBDDeviceDetail>();

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public List<IrrBDDeviceDetail> getDetails() {
			return details;
		}

		public void setDetails(List<IrrBDDeviceDetail> details) {
			this.details = details;
		}
		
		
		
	}
	
}
